package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.dao.DutyDao;

import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultList;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.company.entity.*;

import com.fz.us.admin.company.service.DutyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类 - 职责
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-02 。
 * ============================================================================
 */
@Service
public class DutyServiceImpl extends BaseServiceImpl<Duty, String> implements DutyService {

    @Resource
    private DutyDao dutyDao;
    @Override
    public BaseDao<Duty, String> getBaseDao() {
        return dutyDao;
    }

    @Override
    public Duty getDefaultDuty(User user) {
        return dutyDao.getDefaultDuty(user);
    }

    @Override
    public boolean hasPower(Power power) {
        return dutyDao.hasPower(power);
    }



    @Override
    public boolean hasDepartment(Department department) {
        return dutyDao.hasDepartment(department);
    }

    @Override
    public boolean hasMainDuty(User user) {
        return dutyDao.hasMainDuty(user);
    }

    @Override
    public List<Duty> getUserDutyList(User user) {
        return dutyDao.getUserDutyList(user);
    }

    @Override
    public Duty getUserDefaultDuty(User user) {
        Duty duty = dutyDao.getUserDefaultDuty(user);
        if (duty == null) {
            List<Duty> dutyList = getUserDutyList(user);
            if (dutyList!=null&&dutyList.size()>0) {
                return dutyList.get(0);
            }
        }
        return duty;
    }

    @Override
    public List<Duty> getDutyListByPower(Power power) {
        return dutyDao.getDutyListByPower(power);
    }

    @Override
    public List<Duty> getDutyListByDepartment(Department department) {
        return dutyDao.getDutyListByDepartment(department);
    }



    @Override
    public List<Duty> getDutyListByPost(Post post) {
        return dutyDao.getDutyListByPost(post);
    }

    @Override
    public List<User> getPowerUserList(Power power) {
        return dutyDao.getPowerUserList(power);
    }

    @Override
    public List<User> getDepartmentUserList(Department department) {
        return dutyDao.getDepartmentUserList(department);
    }

    @Override
    public List<User> getPostUserList(Post post) {
        return dutyDao.getPostUserList(post);
    }

    @Override
    public Result setMainDuty(User user, Duty duty) {
        Assert.notNull(user, "user is required");
        Assert.notNull(duty, "duty is required");

        // 取消用户默认职权
        removeMainDuty(user);

        // 设置默认职权
        duty.setIfMain(1);
        update(duty);

        return resultService.success();
    }

    @Override
    public Result removeMainDuty(User user) {
        Assert.notNull(user, "user is required");

        List<Rule> ruleList = new ArrayList<Rule>();
        ruleList.add(RuleHelper.eq("ifMain", 1));

        SearchResultList resultList = listDuty(ruleList, null, user.getCompany(), user);
        List<Duty> dutyList = resultList.getList();
        for (Duty entity : dutyList) {
            entity.setIfMain(0);
            update(entity);
        }
        return resultService.success();
    }

    @Override
    public Result createEntity(User user, Duty duty) {
        Assert.notNull(user, "user is required");
        Assert.notNull(duty, "duty is required");

        // 设置所属企业
        duty.setCompany(user.getCompany());
        // 默认主要职位
        if(!hasMainDuty(user)) {
            duty.setIfMain(1);
        }

        return saveEntity(duty);
    }

    @Override
    public Result updateEntity(User user, String keyId, Duty duty) {
        Assert.notNull(user, "user is required");
        Assert.hasText(keyId, "keyId is required");
        Assert.notNull(duty, "duty is required");

        Duty dutyTemp = get(keyId);
        dutyTemp.setPower(duty.getPower());
        dutyTemp.setUser(duty.getUser());

        update(dutyTemp);
        return resultService.successWithId(dutyTemp.getId());
    }

    @Override
    public Result methodEntity(User user, String method, String keyId) {
        Assert.notNull(user, "user is required");
        Assert.hasText(method, "method is required");
        Assert.hasText(keyId, "keyId is required");

        Duty duty = get(keyId);
        if (duty != null) {
            if (StringUtils.equals("Delete", method)) {// 删除


                return methodEntity(BaseEnumManager.MethodEnum.Delete, keyId);
            }
        }

        return resultService.fail();
    }

    @Override
    public SearchResultPaging pagingDuty(PagePaging page, List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("power.post.sortNo", SearchEnum.OrderType.asc);

        return findPagingBySearch(page, rules, order);
    }

    @Override
    public SearchResultList listDuty(List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("power.post.sortNo", SearchEnum.OrderType.asc);

        return findListBySearch(rules, order);
    }

    @Override
    public SearchResultPaging pagingDuty(PagePaging page, List<Rule> rules, Order order, Company company, Department department, BaseEnumManager.StateEnum... states) {

        if (rules == null)
            rules = new ArrayList<Rule>();
        if (department != null)
            rules.add(RuleHelper.eq("power.department", department));

        return pagingDuty(page, rules, order, company, states);
    }

    @Override
    public SearchResultPaging pagingDuty(PagePaging page, List<Rule> rules, Order order, Company company, Power power, BaseEnumManager.StateEnum... states) {

        if (rules == null)
            rules = new ArrayList<Rule>();
        if (power != null)
            rules.add(RuleHelper.eq("power", power));

        return pagingDuty(page, rules, order, company, states);
    }

    @Override
    public SearchResultPaging pagingDuty(PagePaging page, List<Rule> rules, Order order, Company company, Post post, BaseEnumManager.StateEnum... states) {
        if (rules == null)
            rules = new ArrayList<Rule>();
        if (post != null)
            rules.add(RuleHelper.eq("power.post", post));

        return pagingDuty(page, rules, order, company, states);
    }

    @Override
    public SearchResultList listDuty(List<Rule> rules, Order order, Company company, Department department, BaseEnumManager.StateEnum... states) {

        if (rules == null)
            rules = new ArrayList<Rule>();
        if (department != null)
            rules.add(RuleHelper.eq("power.department", department));

        return listDuty(rules, order, company, states);
    }

    @Override
    public SearchResultPaging pagingDuty(PagePaging page, List<Rule> rules, Order order, Company company, User user, BaseEnumManager.StateEnum... states) {
        Assert.notNull(user, "user is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("user", user));

        return pagingDuty(page, rules, order, company, states);
    }

    @Override
    public SearchResultList listDuty(List<Rule> rules, Order order, Company company, User user, BaseEnumManager.StateEnum... states) {
        Assert.notNull(user, "user is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("user", user));

        return listDuty(rules, order, company, states);
    }

    @Override
    public Map<String, Object> getListMap(Duty duty) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("id", StringUtils.isEmpty(duty.getId())?"":duty.getId());

        map.put("departmentId", "");
        map.put("departmentName", "");
        map.put("postId", "");
        map.put("postName", "");
        map.put("powerId", "");
        map.put("powerName", "");
        map.put("powerParentId", "");
        map.put("powerParentName", "");
        if (duty.getPower() != null) {
            Power power = duty.getPower();
            if (power.getDepartment() != null) {
                map.put("departmentId", power.getDepartment().getId());
                map.put("departmentName", power.getDepartment().getName());
            }
            if (power.getPost() != null) {
                map.put("postId", power.getPost().getId());
                map.put("postName", power.getPost().getName());
            }
            map.put("powerId", power.getId());
            map.put("powerName", map.get("departmentName")+" - "+map.get("postName"));
            if (power.getParent() != null) {
                Power parent = power.getParent();
                map.put("powerParentId", parent.getId());
                if (parent.getDepartment() != null && parent.getPost() != null) {
                    map.put("powerParentName", parent.getDepartment().getName()+" - "+parent.getPost().getName());
                }
            }
        }
        map.put("dutyState", duty.getDutyState()!=null? duty.getDutyState().getName():"");
        map.put("userId", "");
        map.put("userName", "");
        if (duty.getUser() != null) {
            User user = duty.getUser();
            map.put("userId", user.getId());
            map.put("userName", user.getName());
        }

        // other
        map.put("state", duty.getState()==null?"":duty.getState().name());

        return map;
    }

    @Override
    public Map<String, Object> getDetailMap(Duty duty) {
        return getListMap(duty);
    }

    @Override
    public Duty getPrincipalDuty(Department department) {
        return dutyDao.getPrincipalDuty(department);
    }

    @Override
    public List<Department> getDepartments(User user, Company company) {
        return dutyDao.getDepartments(user,company);
    }

    @Override
    public List<Duty> getDutys(User user) {
        return dutyDao.getDutys(user);
    }

    @Override
    public List<Duty> getDutys(Department department) {
        return dutyDao.getDutys(department);
    }

    @Override
    public List<Duty> getDutys(Department department, User user) {
        return dutyDao.getDutys( department, user);
    }

    @Override
    public List<Department> getPrincipalDepartments(User user) {
        return dutyDao.getPrincipalDepartments(user);
    }

    @Override
    public List<Department> getDepartments(User user, Post post) {
        return dutyDao.getDepartments(user, post);
    }

    @Override
    public List<User> getPersons(Department department) {
        return dutyDao.getPersons(department);
    }

    @Override
    public List<User> getPersons(Department department, Post post) {
        return dutyDao.getPersons(department, post);
    }

    @Override
    public User getPrincipal(Department department) {
        return dutyDao.getPrincipal(department);
    }

    @Override
    public Duty getParentPrincipal(Department department) {
        return dutyDao.getParentPrincipal(department);
    }


}
